package org.black.phoneUtility.sms.reply.app.fragment;

import org.black.phoneUtility.R;
import org.black.phoneUtility.sms.reply.database.dataModel.SmsReply;
import org.black.phoneUtility.sms.reply.util.DateTimeUtil;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;

public class SmsReplyReplyDateTimeExpiredAfterDialogFragment extends
		DialogFragment implements View.OnClickListener {
	public static final int DEFAULT_MINUTES_VALUE = 30;
	public static final int DEFAULT_HOURS_VALUE = 0;

	public static final String TAG = SmsReplyReplyDateTimeExpiredAfterDialogFragment.class
			.getName();
	public static final String ARGUMENTS_REPLY_DATE_TIME_EXPIRED_AFTER = "replyDateTimeExpiredAfter";

	public interface SmsReplyReplyDateTimeExpiredAfterListener {
		public void onReplyDateTimeExpiredAfterPositiveClick(
				SmsReply replyDateTimeModeExpiredAfter);

		public void onReplyDateTimePeriodDialogNegativeClick();
	}

	private TimePicker timePicker;
	private Button thirtyMinutesButton;
	private Button oneHourButton;
	private Button eightHoursButton;
	private SmsReplyReplyDateTimeExpiredAfterListener smsReplyReplyDateTimeExpiredAfterListener;

	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

		LayoutInflater inflater = getActivity().getLayoutInflater();
		View view = inflater.inflate(
				R.layout.sms_reply_reply_date_time_expired_after_dialog, null);

		builder.setView(view);
		this.timePicker = (TimePicker) view
				.findViewById(R.id.smsReplyDateTimeExpiredAfterTimePicker);
		this.timePicker.setIs24HourView(true);
		if (getArguments().getSerializable(
				ARGUMENTS_REPLY_DATE_TIME_EXPIRED_AFTER) != null) {
			SmsReply replyDateTimeModeExpiredAfter = (SmsReply) getArguments()
					.getSerializable(ARGUMENTS_REPLY_DATE_TIME_EXPIRED_AFTER);
			long milliSeconds = replyDateTimeModeExpiredAfter
					.getExpiredAfterMilliSeconds();
			this.timePicker
					.setCurrentHour(Long
							.valueOf(
									DateTimeUtil
											.extractHoursFromDateTimeInMilliSeconds(milliSeconds))
							.intValue());
			this.timePicker
					.setCurrentMinute(Long
							.valueOf(
									DateTimeUtil
											.extractMinutesFromDateTimeInMilliSeconds(milliSeconds))
							.intValue());
		} else {
			this.timePicker.setCurrentMinute(DEFAULT_MINUTES_VALUE);
			this.timePicker.setCurrentHour(DEFAULT_HOURS_VALUE);
		}
		builder.setTitle(R.string.sms_reply_date_time_expired_after)
				.setPositiveButton(android.R.string.ok,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								Object replyDateTimeModeExpiredAfterObject = getArguments()
										.getSerializable(
												ARGUMENTS_REPLY_DATE_TIME_EXPIRED_AFTER);
								SmsReply replyDateTimeModeExpiredAfter = (SmsReply) replyDateTimeModeExpiredAfterObject;
								replyDateTimeModeExpiredAfter
										.setReplyDateTimeModeType(SmsReply.REPLY_DATE_TIME_MODE_EXPIRED_AFTER_TYPE);
								replyDateTimeModeExpiredAfter
										.setReplyDateTimeModeType(SmsReply.REPLY_DATE_TIME_MODE_EXPIRED_AFTER_TYPE);
								replyDateTimeModeExpiredAfter
										.setExpiredAfterMilliSeconds(1000l * (3600l * timePicker
												.getCurrentHour() + 60l * timePicker
												.getCurrentMinute()));
								smsReplyReplyDateTimeExpiredAfterListener
										.onReplyDateTimeExpiredAfterPositiveClick(replyDateTimeModeExpiredAfter);
							}
						}).setNegativeButton(android.R.string.cancel, null);
		this.thirtyMinutesButton = (Button) view
				.findViewById(R.id.smsReplyDateTimeExpiredAfterThirtyMinutesButton);
		this.oneHourButton = (Button) view
				.findViewById(R.id.smsReplyDateTimeExpiredAfterOneHoursButton);
		this.eightHoursButton = (Button) view
				.findViewById(R.id.smsReplyDateTimeExpiredAfterEightHoursButton);
		this.thirtyMinutesButton.setOnClickListener(this);
		this.oneHourButton.setOnClickListener(this);
		this.eightHoursButton.setOnClickListener(this);
		return builder.create();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			smsReplyReplyDateTimeExpiredAfterListener = (SmsReplyReplyDateTimeExpiredAfterListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(
					activity.toString()
							+ " must implement SmsReplyReplyDateTimeExpiredAfterListener");
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		this.timePicker = null;
		this.thirtyMinutesButton = null;
		this.oneHourButton = null;
		this.eightHoursButton = null;
	}

	@Override
	public void onClick(View v) {
		int hours = 0;
		int mins = 0;
		if (v.getId() == R.id.smsReplyDateTimeExpiredAfterThirtyMinutesButton) {
			hours = 0;
			mins = 30;
		} else if (v.getId() == R.id.smsReplyDateTimeExpiredAfterOneHoursButton) {
			hours = 1;
			mins = 0;
		} else if (v.getId() == R.id.smsReplyDateTimeExpiredAfterEightHoursButton) {
			hours = 8;
			mins = 0;
		} else {
			hours = DEFAULT_HOURS_VALUE;
			mins = DEFAULT_MINUTES_VALUE;
		}

		this.timePicker.setCurrentHour(hours);
		this.timePicker.setCurrentMinute(mins);
	}
}
